import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Hero }              from '../../hero';
import { HeroService }       from '../../providers/hero.service.observable';

/*
  Generated class for the HeroObservablePage page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Component({
  templateUrl: 'build/pages/hero-observable/hero-observable.html',
  providers: [HeroService,
    // HTTP_PROVIDERS,
    // { provide: XHRBackend, useClass: InMemoryBackendService }, // in-mem server
    // { provide: SEED_DATA, useClass: HeroData }                // in-mem server data
  ]
})
export class HeroObservablePage {

  errorMessage: string;
  heroes: Hero[];
  mode = 'Observable';

  constructor(private nav: NavController, private heroService: HeroService) {

  }

  ngOnInit() { this.getHeroes(); }

  getHeroes() {
    this.heroService.getHeroes()
      .subscribe(
      heroes => this.heroes = heroes,
      error => this.errorMessage = <any>error);
  }

  addHero(name: string) {
    if (!name) { return; }
    this.heroService.addHero(name)
      .subscribe(
      hero => this.heroes.push(hero),
      error => this.errorMessage = <any>error);
  }
}
